<?php

declare(strict_types=1);

namespace App\Job;

use Hyperf\AsyncQueue\Job;
use Hyperf\Di\Annotation\Inject;
use phpDocumentor\Reflection\Types\ContextFactory;
use Psr\Log\LoggerInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Utils\Coroutine;

class TestJob extends Job
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    public $params = [];
    public function __construct(array $params)
    {
        $this->params = $params;
        $this->logger = ApplicationContext::getContainer()->get(\Hyperf\Logger\LoggerFactory::class)->get('job');
    }

    public function handle()
    {
        echo date('Y-m-d H:i:s') . "exec TestJob start\r\n";

        $cos = [];
        for ($i = 0; $i<10; $i++)
        {
            $cos['s' . $i] = function () use ($i)
            {
                $coid = Coroutine::id();
                echo "exec co {$i} coid: {$coid}\r\n";
                return $coid;
            };
        }

        $results = parallel($cos);

        $this->logger->info(sprintf("TestJob params: %s, result:%s\r\n", json_encode($this->params), json_encode($results)));
        echo "exec TestJob end\r\n";
    }
}
